package com.example.bluetoothdemo;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.*;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.os.IBinder;
import android.os.ResultReceiver;
import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaControllerCompat;
import android.support.v4.media.session.PlaybackStateCompat;
import android.util.Log;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import android.app.PendingIntent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.media.session.MediaSessionCompat;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import static android.content.ContentValues.TAG;
import static android.icu.number.NumberRangeFormatter.with;


public class MainActivity extends AppCompatActivity {
    private String TAG = "ylh";


    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        media();
    }


    MediaSessionCompat ms;
    PendingIntent pendingIntent;
    Intent mediaButtonIntent;
    Intent svc;
    PlaybackStateCompat ps;
    private void media() {
        svc = new Intent(this, BackgroundMusicService.class);
        startService(svc);
        mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        mediaButtonIntent.setComponent(new ComponentName(this, BackgroundMusicService.class));
        pendingIntent = PendingIntent.getBroadcast(
                getBaseContext(),
                0,
                mediaButtonIntent,
                PendingIntent.FLAG_IMMUTABLE
        );

        ms = new MediaSessionCompat(this, "My__MediaSessionTag", null, pendingIntent);
        ms.setActive(true);
        ms.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
        mediaButtonIntent.setClass(this, MediaButtonReceiver.class);
        ms.setMediaButtonReceiver(PendingIntent.getBroadcast(this, 100, mediaButtonIntent, PendingIntent.FLAG_IMMUTABLE));
        ps = new PlaybackStateCompat.Builder().setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE
                        | PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE |
                        PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
                        PlaybackStateCompat.ACTION_FAST_FORWARD | PlaybackStateCompat.ACTION_REWIND)
                .setState(PlaybackStateCompat.STATE_PAUSED, 0 /*PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN*/, 1f)
                .build();
        ms.setPlaybackState(ps);
        ms.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS);

        //应用在前台、后台（活动状态）会触发这里的事件
        ms.setCallback(new MediaSessionCompat.Callback() {
            @Override
            public void onCommand(String command, Bundle extras, ResultReceiver cb) {
                Log.d(TAG, "onCommand: ");
            }

            @Override
            public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
                Log.d(TAG, "onMediaButtonEvent: ");

                return true;
            }

            @Override
            public void onPlay() {
                Log.d(TAG, "onPlay: ");
            }

            @Override
            public void onSkipToQueueItem(long id) {
                Log.d(TAG, "onSkipToQueueItem: ");
            }

            @Override
            public void onPause() {
                Log.d(TAG, "onPause: ");
            }

            @Override
            public void onSkipToNext() {
                Log.d(TAG, "onSkipToNext: ");
            }

            @Override
            public void onSkipToPrevious() {
                Log.d(TAG, "onSkipToPrevious: ");
            }

            @Override
            protected void finalize() {
                Log.d(TAG, "finalize: ");
            }

            @Override
            public void onStop() {
                Log.d(TAG, "onStop: ");
            }
        });
    }

}